package com.hlx.service.user.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hlx.entity.admin.Station;
import com.hlx.entity.admin.TrainNumber;
import com.hlx.entity.user.TrainNumberStation;
import com.hlx.mapper.user.TrainNumberStationMapper;
import com.hlx.service.user.TrainNumberStationService;
import com.hlx.utils.TimeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalTime;
import java.util.Comparator;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class TrainNumberStationServiceImpl extends ServiceImpl<TrainNumberStationMapper, TrainNumberStation> implements TrainNumberStationService {

    @Override
    public List<TrainNumberStation> getTrainNumberStationByStationName(String stationName) {
        LambdaQueryWrapper<TrainNumberStation> queryWrapperT = new LambdaQueryWrapper<>();
        queryWrapperT.eq(TrainNumberStation::getStationName,stationName);
        List<TrainNumberStation> tns = this.list(queryWrapperT);
        Date date = new Date();
        return tns;
    }

    @Override
    public List<String> getTrainNumberByTwoName(List<String> stationName) {
        LambdaQueryWrapper<TrainNumberStation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(TrainNumberStation::getStationName,stationName);
        queryWrapper.groupBy(TrainNumberStation::getTrainNumberName);
        queryWrapper.having("COUNT(train_number_name) = 2");
        List<TrainNumberStation> tns = this.list(queryWrapper);
        if(tns.isEmpty()){
            return null;
        }
        System.out.println("这里是车次："+tns.stream().map(item->item.getTrainNumberName()).collect(Collectors.toList()));
        return tns.stream().map(item->item.getTrainNumberName()).collect(Collectors.toList());
    }

    @Override
    public TrainNumberStation getTrainNumberByStationNameAndTrainNumber(String stationName, String trainNumber) {
        LambdaQueryWrapper<TrainNumberStation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TrainNumberStation::getStationName,stationName);
        queryWrapper.and((wrapper)->wrapper.eq(TrainNumberStation::getTrainNumberName,trainNumber));
        return this.getOne(queryWrapper);
    }

    @Override
    public Integer getStopTime(List<TrainNumberStation> trainNumberStations) {
        Iterator<TrainNumberStation> it = trainNumberStations.iterator();
        Integer stopTime = 0;
        while(it.hasNext()){
            stopTime += it.next().getStopTime();
        }
        return stopTime;
    }

    @Override
    public Boolean addTrainNumberStations(List<Station> stations, TrainNumber trainNumber,Integer velocity,List<Integer> stopTime) {
//        插入出发车站
//        第index站
        Integer index = 1;
        TrainNumberStation trainNumberStationF = new TrainNumberStation();
        trainNumberStationF.setTrainNumberName(trainNumber.getTrainNumberName());
        trainNumberStationF.setStationName(trainNumber.getStationStart());
        trainNumberStationF.setStartTime(trainNumber.getStartTime());
        trainNumberStationF.setArriveTime(LocalTime.of(00,00,00));
        trainNumberStationF.setStopTime(0);
        trainNumberStationF.setStationNumber(index);
        this.save(trainNumberStationF);
//        插入经停站
        LocalTime timeLast = trainNumberStationF.getStartTime();
        Integer distance = 0;
        TimeUtils timeUtils = new TimeUtils();
        LocalTime startTime = trainNumberStationF.getStartTime();
        Iterator<Integer> it = stopTime.iterator();
        if(trainNumber.getSequence() == 1){
            stations.sort(Comparator.comparing(Station::getDistanceBeijing));
        }else{
            stations.sort(Comparator.comparing(Station::getDistanceBeijing).reversed());
        }


        for(int i = 0; i < stations.size();i++){
            log.info("{}",stations.get(i));
        }
        while (index < stations.size()-1){
            System.out.println(index);
            TrainNumberStation trainNumberStation = new TrainNumberStation();
            if (stations.get(index).getDistanceBeijing() > stations.get(index-1).getDistanceBeijing()){
                distance = stations.get(index).getDistanceBeijing() - stations.get(index-1).getDistanceBeijing();
            }else {
                distance = stations.get(index-1).getDistanceBeijing() - stations.get(index).getDistanceBeijing();
            }
//            System.out.println(index);
            trainNumberStation.setTrainNumberName(trainNumber.getTrainNumberName());
            trainNumberStation.setStationName(stations.get(index).getStationName());
//            System.out.println(index);
            trainNumberStation.setArriveTime(timeUtils.calculateTime(distance,velocity,timeLast));
            trainNumberStation.setStopTime(it.next());
            trainNumberStation.setStartTime(trainNumberStation.getArriveTime().plusMinutes(trainNumberStation.getStopTime()));
            timeLast = trainNumberStation.getStartTime();
            trainNumberStation.setStationNumber(index++);
//            System.out.println(index);
            this.save(trainNumberStation);
        }
//        插入终点站
        TrainNumberStation trainNumberStationFin = new TrainNumberStation();
        trainNumberStationFin.setTrainNumberName(trainNumber.getTrainNumberName());
        trainNumberStationFin.setStationName(trainNumber.getStationEnd());
        trainNumberStationFin.setStartTime(LocalTime.of(00,00,00));
        trainNumberStationFin.setArriveTime(trainNumber.getEndTime());
        trainNumberStationFin.setStopTime(0);
        trainNumberStationFin.setStationNumber(index);
        this.save(trainNumberStationFin);
        return true;
    }

    @Override
    public boolean deleteTrainNumberStations(String trainNumberName) {
        LambdaQueryWrapper<TrainNumberStation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TrainNumberStation::getTrainNumberName,trainNumberName);
        return this.remove(queryWrapper);
    }
}
